package com.lcq.study.pdf.jasper;

import com.lcq.study.pdf.dto.CarbonEmissionDataDto;
import com.lcq.study.pdf.dto.EnergyConsumptionDataDto;
import com.lcq.study.pdf.dto.ModelTableSource;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName JasperReportTest
 * @Description
 * @Author lichuanqi
 * @Date 2022/6/24 10:07
 * @Version 1.0
 **/
public class ECTest {
    public static void main(String[] args) throws JRException {


        String jasperFilePath = "C:/Users/86156/JaspersoftWorkspace/MyReports";

        String name1 = "BPAReport_12";

        String name2 = "BPAReport_13";

        JasperFillManager jasperFillManager = JasperFillManager.getInstance(DefaultJasperReportsContext.getInstance());
        JasperPrint jasperPrint1 = jasperFillManager.fill(getJasperFile(jasperFilePath, name1),
                new HashMap<>(),
                new JRBeanCollectionDataSource(getDataSource(getData1List())));

        JasperPrint jasperPrint2 = jasperFillManager.fill(getJasperFile(jasperFilePath, name2),
                new HashMap<>(),
                new JRBeanCollectionDataSource(getDataSource(getData2List())));

        List<JasperPrint> jasperPrintList = new ArrayList<>();
        jasperPrintList.add(jasperPrint1);
        jasperPrintList.add(jasperPrint2);

        String destFile = "D:/test/pdf/ec.pdf";

        JasperUtils.exportPages(destFile, jasperPrintList);
    }

    private static List<ModelTableSource> getDataSource(Collection<?> beanCollection) {
        ModelTableSource mts = new ModelTableSource();
        mts.setTableData(new JRBeanCollectionDataSource(beanCollection));
        List<ModelTableSource> mlist = new ArrayList<>();
        mlist.add(mts);
        return mlist;
    }

    private static String getJasperFile(String jasperFilePath, String name) {
        return jasperFilePath + "/" + name + ".jasper";
    }
    public static List<EnergyConsumptionDataDto> getData1List(){
        ArrayList<EnergyConsumptionDataDto> list = new ArrayList<>();

        for (int i = 1; i <= 12; i++) {
            EnergyConsumptionDataDto dataDto = new EnergyConsumptionDataDto();
            dataDto.setMonth(""+i);
            dataDto.setMonthColdFh("9.8");
            dataDto.setMonthColdNh("2.8");
            dataDto.setMonthHeatFh("10077");
            dataDto.setMonthHeatNh("3875.6");
            list.add(dataDto);
        }


        return list;
    }

    public static List<CarbonEmissionDataDto> getData2List(){
        ArrayList<CarbonEmissionDataDto> list = new ArrayList<>();

        for (int i = 1; i <= 6; i++) {
            CarbonEmissionDataDto dataDto = new CarbonEmissionDataDto();
            dataDto.setMonth1(""+i);
            dataDto.setCarbonEmission1("4892.733564");
            dataDto.setMonth2(""+(i+6));
            dataDto.setCarbonEmission2("8511.065456");
            list.add(dataDto);
        }


        return list;

    }
}
